//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AmericanPayoffAtHit.h"
using namespace Cephei::QL::Pricingengines;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (Double spot, Double discount, Double dividendDiscount, Double variance, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CStrikedTypePayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phAmericanPayoffAtHit = NULL;
#endif
        QuantLib::Real _spot = (QuantLib::Real)ValueHelper::Convert (spot);
        QuantLib::DiscountFactor _discount = (QuantLib::DiscountFactor)ValueHelper::Convert (discount);
        QuantLib::DiscountFactor _dividendDiscount = (QuantLib::DiscountFactor)ValueHelper::Convert (dividendDiscount);
        QuantLib::Real _variance = (QuantLib::Real)ValueHelper::Convert (variance);
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _ppAmericanPayoffAtHit = new boost::shared_ptr<QuantLib::AmericanPayoffAtHit> (new QuantLib::AmericanPayoffAtHit ( _spot,  _discount,  _dividendDiscount,  _variance,  _payoff ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (boost::shared_ptr<QuantLib::AmericanPayoffAtHit>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtHit = NULL;
#endif
	_ppAmericanPayoffAtHit = &childNative;
    
}
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (QuantLib::AmericanPayoffAtHit& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAmericanPayoffAtHit = NULL;
#endif
	_ppAmericanPayoffAtHit = new boost::shared_ptr<QuantLib::AmericanPayoffAtHit> (&childNative);
    
    _AmericanPayoffAtHitOwner = owner;
    
}

Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (CAmericanPayoffAtHit^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtHit = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanPayoffAtHit = new boost::shared_ptr<QuantLib::AmericanPayoffAtHit> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtHit = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanPayoffAtHit::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (QuantLib::Handle<QuantLib::AmericanPayoffAtHit>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPayoffAtHit = &childNative;
	_ppAmericanPayoffAtHit = &static_cast<boost::shared_ptr<QuantLib::AmericanPayoffAtHit>>(childNative.currentLink());
    
    _AmericanPayoffAtHitOwner = owner;
}
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (QuantLib::Handle<QuantLib::AmericanPayoffAtHit> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanPayoffAtHit = &childNative;
	_ppAmericanPayoffAtHit = &static_cast<boost::shared_ptr<QuantLib::AmericanPayoffAtHit>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::CAmericanPayoffAtHit (QuantLib::AmericanPayoffAtHit childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanPayoffAtHit = NULL;
#endif
	_ppAmericanPayoffAtHit = new boost::shared_ptr<QuantLib::AmericanPayoffAtHit> (new QuantLib::AmericanPayoffAtHit (childNative));
    
}
#endif

Cephei::QL::Pricingengines::CAmericanPayoffAtHit::~CAmericanPayoffAtHit ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAmericanPayoffAtHit != NULL)
    {
	    delete _ppAmericanPayoffAtHit;
        _ppAmericanPayoffAtHit = NULL;
    }
}
Cephei::QL::Pricingengines::CAmericanPayoffAtHit::!CAmericanPayoffAtHit ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAmericanPayoffAtHit != NULL)
    {
	    delete _ppAmericanPayoffAtHit;
    }
}
QuantLib::AmericanPayoffAtHit& Cephei::QL::Pricingengines::CAmericanPayoffAtHit::GetReference ()
{
    if (_ppAmericanPayoffAtHit == NULL) throw gcnew NativeNullException ();
	return **_ppAmericanPayoffAtHit;
}
boost::shared_ptr<QuantLib::AmericanPayoffAtHit>& Cephei::QL::Pricingengines::CAmericanPayoffAtHit::GetShared ()
{
    if (_ppAmericanPayoffAtHit == NULL) throw gcnew NativeNullException ();
	return *_ppAmericanPayoffAtHit;
}
QuantLib::AmericanPayoffAtHit* Cephei::QL::Pricingengines::CAmericanPayoffAtHit::GetPointer ()
{
    if (_ppAmericanPayoffAtHit == NULL) throw gcnew NativeNullException ();
	return &**_ppAmericanPayoffAtHit;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanPayoffAtHit>& Cephei::QL::Pricingengines::CAmericanPayoffAtHit::GetHandle ()
{
	if (_phAmericanPayoffAtHit == NULL)
	{
		_phAmericanPayoffAtHit = new Handle<QuantLib::AmericanPayoffAtHit> (*_ppAmericanPayoffAtHit);
	}
	return *_phAmericanPayoffAtHit;
}
#endif
bool Cephei::QL::Pricingengines::CAmericanPayoffAtHit::HasNative () 
{
	return (_ppAmericanPayoffAtHit != NULL);
}

Double Cephei::QL::Pricingengines::CAmericanPayoffAtHit::Delta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPayoffAtHit)->delta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CAmericanPayoffAtHit::Gamma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPayoffAtHit)->gamma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CAmericanPayoffAtHit::Rho (Double maturity)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPayoffAtHit)->rho ( _maturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Pricingengines::CAmericanPayoffAtHit::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAmericanPayoffAtHit)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::IAmericanPayoffAtHit^ Cephei::QL::Pricingengines::CAmericanPayoffAtHit_Factory::Create (Double spot, Double discount, Double dividendDiscount, Double variance, Cephei::QL::Instruments::IStrikedTypePayoff^ payoff)
{
    return gcnew CAmericanPayoffAtHit ( spot,  discount,  dividendDiscount,  variance,  payoff);
}
